package come.bite.spring.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/Request")
@RestController
public class Comtreatel {
    @RequestMapping("/m1")    //传递单个参数
    public String user(String user){
        return user;
    }


    @RequestMapping("/m2")  //传递多个参数
    public String user2(String name,String password){
        return "name:"+name+"password+:"+password;
    }


    @GetMapping("/m3")
    public String user3(String name){
        return "name:"+name;
    }


    @RequestMapping(value = "/m4",method = RequestMethod.GET)  //指定请求类型
    public String user4(String name){
        return "name:"+name;
    }


    @RequestMapping("/m5")
        public String user5(user user){
        return "接收参数 userinfo"+ user.toString();
        }


        @RequestMapping("/m6")
    public String user6(@RequestParam(value = "q",required = false) String keyword){
        return "keyword:"+keyword;    //  @RequestParam 表示给参数重命名
        }

    @RequestMapping("/m7")       //传递数组
    public String user7(String[] arr){
        return "接收参数 arr=："+Arrays.toString(arr);
    }

    @RequestMapping("/m8")       //传递List
    public String user8(@RequestParam List<Integer> list){
        return "接收参数 list:"+list;
    }

    @RequestMapping("/m9")     //传递json对象
    public String user9(@RequestBody user user){  //@RequestBody注解 指用传递json对象
        return user.toString();
    }

    //从url中获取参数
    @RequestMapping("/articleId/{articleId}")
    public String urser10(@PathVariable Integer articleId){ //@PathVariable注解是将
        return "获取文章Id :"+articleId;                      //从url路径获取参数并将其
                                                            //绑定在控制器方法参数上面
    }

    //从url中获取多个参数
    @RequestMapping("/articleId/{type}/{articleId}")       //可以进行重命名
    public String urser11(@PathVariable String type,@PathVariable("id") Integer articleId){
        return "获取文章类型"+type+"+获取文章Id"+ articleId;
    }

    //上传文件
    @RequestMapping("/m12")//@RequestPart 对文件重命名
    public String user12(@RequestPart("filler") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());

        //文件上传
        file.transferTo(new File("D:\\db"+file.getOriginalFilename()));
        return "文件获取成功";
    }

    //获取cooking
    @RequestMapping("/m13")   //HttpServletRequest http请求 ，HttpSerletResponse http响应
    public  String user13(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();//HttpServletRequest 和HttpServletResponse 不是成对出现的，用哪个加哪个
       //获取cookies ,cookies 是一个数组
        if (cookies != null){
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "返回cookies 成功";
    }

    //通过注解获取cookies
    @RequestMapping("/m14")
    public  String user14(@CookieValue("java") String java){  //实际开发中并不常见，普遍通常用上面方式获得
        return "从Cookies中获取cookies的值："+java;
    }


    @RequestMapping("/setSession")
    public  String setSession(HttpServletRequest request){
        //从cookie中湖获取sessionid ，根据sessionid 获取session对象
        //sessionId 如果不存在则创建 ...
        HttpSession session = request. getSession();
        //默认存储在内存中
        //登录的用户的名称
       session.setAttribute("userName", "zhangsan");
       session.setAttribute("age", "18");
       return "设置session成功";
    }

    @RequestMapping("/getSession")  //获取session
    public  String getSession(HttpServletRequest request){
        //从cookie中湖获取sessionid ，根据sessionid 获取session对象
        HttpSession session = request.getSession(false);
        //如果用户登录，session 有值，未登录 session 为 null
        if(session != null){
            //从session 中获取用户信息
            String userName = (String)session.getAttribute("userName").toString();
            return "登录用户为："+userName;
        }else {
            return"用户未登录";
        }
    }

    @RequestMapping("/getSession2")  //获取session 第二种方式
    public  String getSession(HttpSession session){  //这行代码等同于 HttpSession session = request.getSession();
        //如果用户登录，session 有值，未登录 session 为 null
            String userName = (String)session.getAttribute("userName").toString();
            return "登录用户为："+userName;
    }

    @RequestMapping("/getSession3")  //获取session 第三种方式
    public  String getSession(@SessionAttribute("userName") String userName){ //这个注解 @SessionAttribute等同于
        //如果用户登录，session 有值，未登录 session 为 null                       //  String userName = (String)session.getAttribute("userName").toString();
        return "登录用户为："+userName;
    }

    @RequestMapping("/getHeader")
    public  String getheader(HttpServletRequest request){
         String userAgent = request.getHeader("User-Agent");
         return "从header 中获取 userAgent:"+userAgent;
    }

    @RequestMapping("/getHeader2")
    public  String getheader2(@RequestHeader("User-Agent") String userAgent){
        //String userAgent = request.getHeader("User-Agent");
        return "从header 中获取 userAgent:"+userAgent;
    }
}
